//+------------------------------------------------------------------+
//|                                                        trend.mq4 |
//|                                         Dimitri Rudel 28.10.2013 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Dimitri Rudel 28.10.2013"
#property link      ""

#include <basic.mqh>
#include <tradeManager.mqh>
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
datetime oldTime;

bool firstCross = false;


extern int exCntMinBar = 10;
extern int exPointDistance = 1;
extern int exCntBreak = 1;
extern double exTakeProfit = 100;

extern double exRetracement = 0.61;

double high;
double low;
double tmpLow;

int barHigh = 0;
int barLow = 0;
datetime dtHigh = 0;
datetime dtHigh1 = 0;
datetime dtLow = 0;
datetime dtLow1 = 0;


int cntTrend = 1;

int cntBreak = 0;

int cntLow = 1;
int cntHigh = 1;

int shiftHigh = 0;
int shiftLow = 0;

double gap = 160;

int trend = 0;
bool newSR = false;

int row = 1;

double move = 0;  
double retracement = 0;
double tmpRetracement = 0; 

int init()
  {
//----
       b_debug("trend");
      oldTime = Time[0];
      dtHigh = Time[0];
      dtLow = Time[0];
      
      low = Low[0] - Point * 10;
      high = High[0] + Point * 10;
      tmpLow = low;
      move = high - low;
      

//----
   return(0);
  }

int start()
  {   
      row = 1; 
      row = draw_new_row("trend",trend,row);
      row = draw_new_row("high",DoubleToStr(high,Digits),row);
      row = draw_new_row("low",DoubleToStr(low,Digits),row);
      row = draw_new_row("tmpLow",DoubleToStr(tmpLow,Digits),row);
      row = draw_new_row("move",DoubleToStr(move,Digits),row);
      row = draw_new_row("retracement",DoubleToStr(retracement,Digits),row);
      //row = draw_new_row("dtHigh",TimeToStr(dtHigh,TIME_MINUTES),row);
      //row = draw_new_row("dtLow",TimeToStr(dtLow,TIME_MINUTES),row);
      
      
     if(Bars >= exCntMinBar){
     }
  
  
     if(Bars < exCntMinBar){
         return(0);
     }
     if(oldTime == Time[0] ){
         return(0);   
     }
     oldTime = Time[0];
     
     if(!b_isTradeTime()){
         //return(0);
      }
 
     //tmgr_checkExit();
     //trend
     if(trend == 0 && Low[1] < low && 0)
     {
       b_alert("Trend is now DOWN");
       trend = 1; 
       drawV(Time[1],"DOWN"+cntTrend);
       cntTrend++;
       cntBreak = 0;
     }
     if(trend == 1 && High[1] > high)
     {
       b_alert("Trend is now UP");
       trend = 0; 
       drawV(Time[1],"UP"+cntTrend);
       cntTrend++; 
       cntBreak = 0;
     }
     if(cntBreak==0){
         for(int k=0; k < OrdersTotal(); k++) {
            if (OrderSelect(k, SELECT_BY_POS, MODE_TRADES)) {
               if ( OrderSymbol()==Symbol()) {
                        OrderDelete(OrderTicket());           
               }
            }
         }
     }



   
     newSR = false;
     int tmpShiftHigh = 0;
     int tmpShiftLow = 0;
     double tmpHigh = 0; 

     

     double tmpMove = 0;
     bool retrace = false;
      
     bool newHigh = false;
     bool newLow = false;
     
     if(Low[1] < tmpLow){
         tmpLow = Low[1];
         dtLow = Time[1];
         cntLow++;
         newLow = true;
              
     }    
     
     if(High[1] > tmpHigh){
         tmpHigh = High[1];
         dtHigh = Time[1];
         cntHigh++;
         
         if(tmpHigh > high){
            newHigh = true;
         }
         //double tmpRetracement = (high - tmpLow)/move;        
     }   
     
     if(newHigh){
         low = tmpLow;
     }
     
     
     if(newLow){
         tmpRetracement = (tmpHigh - tmpLow)/move;
         if(tmpRetracement >= exRetracement){
            high = tmpHigh;        
         }    
     }
     

     if(tmpHigh > high){
     
         move = MathAbs(high - low);
         retracement = (high - tmpLow)/move;
         

         high = tmpHigh;
         //search new low if retracement was enough
         color c = Orange;
         double drawRet = retracement;
         if(retracement >= exRetracement){
            
            retrace = true;
            low = tmpLow;
            retracement = 0;
            c = Red;   
         }else{
            
         }
         
         
         drawA(tmpLow - gap * Point,dtLow,"tmpLow: "+cntLow+" ret: "+drawRet,SYMBOL_ARROWDOWN,c);
         drawA(tmpHigh + gap * Point,Time[1],"high: "+cntHigh+" move: "+move, SYMBOL_ARROWUP, Green);
         
         //reinit
         tmpLow = Low[1];
         dtLow = Time[1];
         
         
     }
     
     return (0);
     if(trend == 0){
         
         tmpHigh = High[1];
                 
         if(tmpHigh > high){           
                       
            if(tmpLow > high){
              //return(1);+
            }
            
            tmpMove = MathAbs(tmpHigh - tmpLow);

            
               
           

            if(retracement >= exRetracement){
               cntHigh++;
               high = tmpHigh; 
               low = tmpLow;
               newSR = true;
               
               dtHigh1 = dtHigh;
               dtHigh = Time[tmpShiftHigh];
               dtLow = Time[tmpShiftLow];
               

               
                                
               cntBreak++;
            
               for(int i=0; i < OrdersTotal(); i++) {
                  if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
                     if ( OrderSymbol()==Symbol()) {
                        b_OrderModify(OrderTicket(),OrderOpenPrice(),low,OrderTakeProfit(),0,Blue);             
                     }
                  }
               }
            //no retracement also it have to be movement
            }else{
            //so the last high wasnt a high
              if(shiftLow < shiftHigh){
                  dtHigh = dtHigh1;
              }
            }    
         }
      //TREND DOWN   
     }else if (trend == 1){
         shiftLow = iBarShift(Symbol(),0,dtLow);
         //tmpShiftLow = iLowest(Symbol(),0,MODE_LOW,shiftLow,1);
         tmpLow = Low[1];

         
         if(tmpLow < low){           

            tmpShiftHigh = iHighest(Symbol(),0,MODE_HIGH,shiftLow,1);    
            //Movement or really new low Point ?
            tmpHigh = High[tmpShiftHigh];
            
            if(tmpHigh > low){
              //return(1);
            }
            move = MathAbs(high - low);
            tmpMove = MathAbs(tmpHigh - tmpLow);
            retracement = (tmpHigh - low)/move; 

            if(retracement >= exRetracement){


               cntHigh++;
               high = tmpHigh; 
               low = tmpLow;
               newSR = true;
               
               dtLow1 = dtLow;
               dtLow = Time[tmpShiftLow];
               dtHigh = Time[tmpShiftHigh];
               

               drawA(low - gap * Point,dtLow,"move "+tmpMove+"   "+cntHigh,SYMBOL_ARROWDOWN,Red);
               drawA(high + gap * Point,dtHigh,"ret : "+retracement+"   "+cntHigh, SYMBOL_ARROWUP, Green);                 
               cntBreak++;
            
               for(int j=0; j < OrdersTotal(); j++) {
                  if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
                     if ( OrderSymbol()==Symbol()) {
                        b_OrderModify(OrderTicket(),OrderOpenPrice(),high,OrderTakeProfit(),0,Blue);             
                     }
                  }
               }
            //no retracement also it have to be movement
            }else{
            //so the last low wasnt a low
              if(shiftLow > shiftHigh){
                  dtLow = dtLow1;
              }
            }    
         }
      //TREND DOWN   
     }
     
     if(cntBreak == exCntBreak && newSR && b_isTradeTime()){
         //double ma = iMA(Symbol(),0,90,0,MODE_SMA,PRICE_CLOSE,1);
         double price = 0;
         double sl = 0;
         int cmd = -1;
         double tp;
         
         if(trend== 0 ){
            price = high + Point * 20;
            sl = low;
            cmd = OP_BUYSTOP;
            tp = price + Point *exTakeProfit;
            
         }else if(trend == 1){
            price = low;
            sl = high + Point * 20;
            cmd = OP_SELLSTOP;
            tp = price - Point *exTakeProfit;
         }else{
            return(1);
         }
         b_OrderSend(Symbol(),cmd,0.1,price,3,sl,0,"trend",0,0); 
     }

}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
  
void drawA(double price,datetime dt,string name = "arrow", int arrowCode = SYMBOL_ARROWUP, color col = Black){  
   ObjectCreate(name,OBJ_ARROW,0,dt,price);  
   ObjectSet(name,OBJPROP_COLOR,col);
   ObjectSet(name,OBJPROP_STYLE,STYLE_SOLID); 
   ObjectSet(name, OBJPROP_ARROWCODE, arrowCode);
}

void drawV(datetime dt,string name = "line",color col = Black){  
   
      ObjectCreate(name,OBJ_VLINE,0,dt,0);
      ObjectSet(name,OBJPROP_STYLE,STYLE_DOT);  
      ObjectSet(name,OBJPROP_COLOR,col);
}
  
void drawH(double price,string name = "line",int type = OBJ_HLINE, color col = Black){     
      ObjectDelete(name);
      ObjectCreate(name,OBJ_HLINE,0,0,price);
      ObjectSet(name,OBJPROP_STYLE,STYLE_DASH);  
      ObjectSet(name,OBJPROP_COLOR,col);
}



int draw_new_row(string col1, string col2,int row)
{
        if(!exDebug){
            return;
        }
        string objName = "a"+row;
        color c= Black;
        int corner_position = 1;
        int fontsize = 16;
        int col1_xdist = 50;
        int y_offset = 5;
        int col2_xdist = 2;
        string fontname = "Times New Roman";


        ObjectCreate(objName,OBJ_LABEL,0,0,0);
        ObjectSet(objName,OBJPROP_CORNER,corner_position);
        ObjectSet(objName,OBJPROP_XDISTANCE,col1_xdist+(fontsize*2.7));
        ObjectSet(objName,OBJPROP_YDISTANCE,row*(fontsize+2)+y_offset+50);
        ObjectSetText(objName,col1,fontsize,fontname,c);
        

         objName = "b"+row;

        c=Red;

        ObjectCreate(objName,OBJ_LABEL,0,0,0);
        ObjectSet(objName,OBJPROP_CORNER,corner_position);
        ObjectSet(objName,OBJPROP_XDISTANCE,col2_xdist);
        ObjectSet(objName,OBJPROP_YDISTANCE,row*(fontsize+2)+y_offset+50);
        ObjectSetText(objName,col2,fontsize,fontname,c);
        
        return(row+1);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+